package com.youha.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.org.apache.xpath.internal.SourceTree;
import com.youha.pojo.User;
import com.youha.pojo.VO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import sun.awt.SunHints;
import sun.management.Agent;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author youha
 * @create 2021-08-17 22:34
 */

@Controller
@RequestMapping("/user")
public class UserController {



    //上传多个文件
    @RequestMapping(value = "/userController33")
    @ResponseBody
    public void save33(String username, String password, MultipartFile[] upload) throws IOException {
        System.out.println(username);
        System.out.println(password);

        for (int i = 0; i < upload.length; i++) {
            String filename = upload[i].getOriginalFilename();
            upload[i].transferTo(new File("D:\\A\\" + filename));
        }
    }

    //上传单个文件
    @RequestMapping(value = "/userController32")
    @ResponseBody
    public void save32(String username, String password, MultipartFile upload, MultipartFile upload2, MultipartFile upload3) throws IOException {
        System.out.println(username);
        System.out.println(password);

        String filename = upload.getOriginalFilename();
        upload.transferTo(new File("D:\\A\\" + filename));

        String filename2 = upload2.getOriginalFilename();
        upload2.transferTo(new File("D:\\A\\" + filename2));

        String filename3 = upload3.getOriginalFilename();
        upload3.transferTo(new File("D:\\A\\" + filename3));

    }


    //@CookieValue获得Cookie的JSESSIONID
    //    JSESSIONID
    @RequestMapping(value = "/userController31")
    @ResponseBody
    public void save31(@CookieValue(value = "JSESSIONID") String jsessionId) throws IOException {
        System.out.println(jsessionId);
    }


    //@RequestHeader获得请求头的数值
    @RequestMapping(value = "/userController30")
    @ResponseBody
    public void save30(@RequestHeader(value = "User-Agent", required = false) String user_agent) throws IOException {
        System.out.println(user_agent);
    }


    //获得Servlet中的API，request、response、session等等
    @RequestMapping(value = "/userController29")
    @ResponseBody
    public void save29(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
        System.out.println(request);
        System.out.println(response);
        System.out.println(session);
    }


    //自定义类型转换器,告诉springMVC
    @RequestMapping(value = "/userController28/{date}")
    @ResponseBody
    public void save28(@PathVariable(value = "date", required = false) Date date) throws IOException {
        System.out.println(date);
    }

    //@PathVariable配置参数的地址访问，不需要使用问号访问
    @RequestMapping(value = "/userController27/{name}/{age}")
    @ResponseBody
    public void save27(@PathVariable(value = "name") String name, @PathVariable(value = "age") int age) throws IOException {
        System.out.println(name);
        System.out.println(age);
    }

    //@RequestParam参数绑定注解
    @RequestMapping(value = "/userController26")
    @ResponseBody
    public void save26(@RequestParam(value = "user", required = false) String username) throws IOException {
        System.out.println(username);
    }

    //
    @RequestMapping(value = "/userController25")
    @ResponseBody //告诉spring我不是需要页面跳转，需要的是回写数据
    public void save25(@RequestBody List<User> userList) throws IOException {
        System.out.println(userList);
    }

    @RequestMapping(value = "/userController24")
    @ResponseBody //告诉spring我不是需要页面跳转，需要的是回写数据
    public void save24(VO vo) throws IOException {
        System.out.println(vo);
    }


    @RequestMapping(value = "/userController23")
    @ResponseBody //告诉spring我不是需要页面跳转，需要的是回写数据
    public void save23(String[] str) throws IOException {
        System.out.println(Arrays.toString(str));
    }


    @RequestMapping(value = "/userController22")
    @ResponseBody //告诉spring我不是需要页面跳转，需要的是回写数据
    public void save22(User user) throws IOException {
        System.out.println(user);
    }

    @RequestMapping(value = "/userController21")
    @ResponseBody //告诉spring我不是需要页面跳转，需要的是回写数据
    public void save21(String name, int age) throws IOException {
        System.out.println(name);
        System.out.println(age);
    }

    @RequestMapping(value = "/userController8")
    @ResponseBody //告诉spring我不是需要页面跳转，需要的是回写数据
    public List<User> save8() throws IOException {
        User user = new User();
        user.setName("xuxiufeng^^");
        user.setAge(22);

        List list = new ArrayList();
        list.add(user);
        list.add(new User("youha", 23));
        return list;
    }

    @RequestMapping(value = "/userController10")
    @ResponseBody
    public User save10() throws IOException {
        User user = new User();
        user.setName("xuxiufeng^^");
        user.setAge(22);

        return user;
    }

    @RequestMapping(value = "/userController9")
    @ResponseBody
    public String save9() throws IOException {
        User user = new User();
        user.setName("xuxiufeng");
        user.setAge(21);

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(user);

        return json;
    }


    @RequestMapping(value = "/userController7")
    @ResponseBody
    public String save7() throws IOException {
        return "youha123";
    }

    @RequestMapping(value = "/userController6")
    public void save6(HttpServletResponse response) throws IOException {

        response.setCharacterEncoding("UTF-8");

        response.getWriter().write("youha");

    }


    @RequestMapping(value = "/userController5")
    public String save5(HttpServletRequest request) {
        request.setAttribute("username", "台历");
        return "youha";
    }

    @RequestMapping(value = "/userController4")
    public String save4(Model model) {
        model.addAttribute("username", "朱文龙 ");
        return "youha";
    }

    @RequestMapping(value = "/userController3")
    public ModelAndView save3(ModelAndView modelAndView) {
        modelAndView.setViewName("youha");
        modelAndView.addObject("username", "youha");
        System.out.println("save^^");
        return modelAndView;
    }

    @RequestMapping(value = "/userController2")
    public ModelAndView save2() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("youha");
        modelAndView.addObject("username", "xuxiufeng");
        System.out.println("save^^");
        return modelAndView;
    }

    @RequestMapping(value = "/userController", method = RequestMethod.GET, params = {"username"})
    public String save() {
        System.out.println("UserController save ^^^");

//        return "/jsp/youha.jsp";

        return "redirect:/jsp/youha.jsp";
//        return "youha";
    }

}
